
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    用户名：<input type="text" name="name" id="name"/><span id="nameSpan"></span><br/>
    密码：<input type="text" name="password" id="password"/><br/>
    <input type="button" value="登录">

   <script src="<%=request.getContextPath()%>/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script>
        $(function() {
            $('#name').blur(function() {
                //this
                $.post(
                    '<%=request.getContextPath()%>/ajax',
                    {'name': $(this).val()},
                    function(jsonObj) {// js对象
                        console.log(jsonObj);
                        if (jsonObj.exist) {
                            $('#nameSpan').html(jsonObj.msg);
                            $('#nameSpan').css('color', 'red');
                        } else {
                            $('#nameSpan').html(jsonObj.msg);
                            $('#nameSpan').css('color', 'green');
                        }
                    },
                    'json'
                );
            });
        });
    </script>

</body>
</html>
